package com.android.tea.shopper;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.tea.R;
import com.android.tea.TeaEncyclopediaApp;
import com.android.tea.api.TeaApi;
import com.android.tea.entity.Clientindustry;
import com.android.tea.widght.SettingItemButton;
import com.android.tea.widght.SettingItemPanel;
import com.android.tea.widght.TEAAlertDialog;

public class ShopperOptionsActivity extends Activity implements OnClickListener {
	LinearLayout shopper_option;
	public static final String SETTING = "settings";
	public static final String SHOCK_STRING = "SHOCK";
	public static final String GANGTAI_STRING = "GANGTAI";
	public static final String SOUND_STRING = "SOUND";
	public static final String RANGE_INT = "RANGE";
	public static final String AVERAGE_INT = "AVERAGE";
	public static final String TEACLASS_KEY = "TEACLASS_KEY";
	public static final String TEACLASS_VALUE = "TEACLASS_VALUE";
	private static final int ID_0 = 1010;
	private static final int ID_1 = 1011;
	private static final int ID_2 = 1012;
	private static final int ID_3 = 1013;
	private static final int ID_4 = 1014;
	private static final int ID_5 = 1015;
	private static final int ID_6 = 1016;
	private static final int ID_7 = 1017;
	private TEAAlertDialog dialog;

	int rangeId = 0;
	
	public static String[] rangeStrings = { "1公里", "2公里", "5公里", "10公里" };
	public static String[] rangeValues = { "1", "2", "5", "10" };

	int averageId = 0;
	public static String[] averageStrings = { "不限价格", "50元以下", "50-200元",
			"200-500元", "500元以上" };
	public static String[] averageValue = { "0", ",50", "50,200", "200,500",
			"500," };

	int teaClassId = 0;
	public static String[] teaClassValue = null;
	public static String[] teaClassKey = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shopper_options);
		
		TeaEncyclopediaApp.list.add(this);
		shopper_option = (LinearLayout) findViewById(R.id.shopper_option);
		init();
		init2();
	}

	SettingItemButton sib, sib1, sib2, sib3, sib4, sib5, sib6, sib7;

	private void init() {
		SettingItemPanel sip = new SettingItemPanel(this);
		sib = new SettingItemButton(this);
		sib.setItemOnClickListener(this);
		sib.setOnOrOff(getBooleanValue(SHOCK_STRING));
		sib.setTitle("一次摇出多个商家");
		sib.setIdUserItem(ID_0);
		sip.addItem(sib);

		sib1 = new SettingItemButton(this);
		sib1.setItemOnClickListener(this);
		sib1.setTitle("选择范围");
		rangeId = getIntValue(RANGE_INT);
		sib1.setMessage(rangeStrings[rangeId]);
		sib1.setIdUserItem(ID_1);
		sip.addItem(sib1);

		sib2 = new SettingItemButton(this);
		sib2.setItemOnClickListener(this);
		sib2.setTitle("选择位置");
		sib2.setIdUserItem(ID_2);
		sip.addItem(sib2);

		sib3 = new SettingItemButton(this);
		sib3.setItemOnClickListener(this);
		sib3.setTitle("人均消费");
		averageId = getIntValue(AVERAGE_INT);
		sib3.setMessage(averageStrings[averageId]);
		sib3.setIdUserItem(ID_3);
		sip.addItem(sib3);

		sib4 = new SettingItemButton(this);
		sib4.setItemOnClickListener(this);
		sib4.setTitle("选择茶系");
		String teaclassValue = getStringValue(TEACLASS_VALUE);
		if (teaclassValue != null)
			sib4.setMessage(teaclassValue);
		sib4.setIdUserItem(ID_4);
		sip.addItem(sib4);

		sib5 = new SettingItemButton(this);
		sib5.setItemOnClickListener(this);
		sib5.setOnOrOff(getBooleanValue(GANGTAI_STRING));
		sib5.setTitle("在港澳台地区使用");
		sib5.setIdUserItem(ID_5);
		sip.addItem(sib5);

		shopper_option.addView(sip);
	}

	private void init2() {
		SettingItemPanel sip = new SettingItemPanel(this);
		sib6 = new SettingItemButton(this);
		sib6.setItemOnClickListener(this);
		sib6.setTitle("添加新商家");
		sib6.setIdUserItem(ID_6);
		sip.addItem(sib6);

		sib7 = new SettingItemButton(this);
		sib7.setItemOnClickListener(this);
		sib7.setOnOrOff(getBooleanValue(SOUND_STRING));
		sib7.setTitle("音效开关");
		sib7.setIdUserItem(ID_7);
		sip.addItem(sib7);

		shopper_option.addView(sip);
	}

	android.content.DialogInterface.OnClickListener aClickListener = new android.content.DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {

			dialog.dismiss();
		}

	};

	@Override
	public void onBackPressed() {
		Activity parent = this.getParent();
		if(parent!=null) {
			parent.onBackPressed();
		}else{
			super.onBackPressed();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case ID_0:
			setBooleanValue(SHOCK_STRING, sib.setChange());
			break;
		case ID_1:
			android.content.DialogInterface.OnClickListener onClickListener = new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					rangeId = which;
					sib1.setMessage(rangeStrings[which]);
					setIntValue(RANGE_INT, rangeId);
					dialog.dismiss();
				}
			};
			dialog = new TEAAlertDialog.Builder(ShopperActivity.instance)
					.setSingleChoiceItems(rangeStrings, rangeId,
							onClickListener).setTitle("选择范围")
					.setPositiveButton("取消", aClickListener).show();

			break;
		case ID_3:
			android.content.DialogInterface.OnClickListener onClickListener2 = new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					averageId = which;
					sib3.setMessage(averageStrings[which]);
					setIntValue(AVERAGE_INT, averageId);
					dialog.dismiss();
				}
			};
			dialog = new TEAAlertDialog.Builder(ShopperActivity.instance)
					.setSingleChoiceItems(averageStrings, averageId,
							onClickListener2).setTitle("选择人均消费")
					.setPositiveButton("取消", aClickListener).show();

			break;
		case ID_4:

			TeaApi.getTeaClass(ShopperActivity.instance,
					new TeaClassInterface() {
						@Override
						public void callbackTeaClass(
								ArrayList<Clientindustry> cList) {

							if (cList == null) {
								Toast.makeText(ShopperActivity.instance,
										"获取茶系失败，请稍后再试。", Toast.LENGTH_LONG)
										.show();
							} else {

								if (teaClassValue == null) {
									int index = 0;
									int count = cList.size();
									teaClassValue = new String[count];
									teaClassKey = new String[count];
									Iterator<Clientindustry> cIterator = cList
											.iterator();
									while (cIterator.hasNext()) {
										Clientindustry clientindustry = (Clientindustry) cIterator
												.next();
										teaClassValue[index] = clientindustry
												.getCoinName();
										teaClassKey[index++] = clientindustry
												.getCoinId();
									}
								}

								android.content.DialogInterface.OnClickListener onClickListener3 = new android.content.DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										teaClassId = which;
										String valueString = teaClassValue[which];
										sib4.setMessage(valueString);
										setStringValue(TEACLASS_VALUE,
												valueString);
										setStringValue(TEACLASS_KEY,
												teaClassKey[which]);
										dialog.dismiss();
									}
								};
								dialog = new TEAAlertDialog.Builder(
										ShopperActivity.instance)
										.setSingleChoiceItems(teaClassValue,
												teaClassId, onClickListener3)
										.setTitle("选择茶系")
										.setPositiveButton("取消", aClickListener)
										.show();
							}

						}
					});

			break;
		case ID_5:
			setBooleanValue(GANGTAI_STRING, sib5.setChange());
			break;
		case ID_6:
			Intent userIntent = new Intent(this, NewShopperActivity.class);
			startActivity(userIntent);
			break;
		case ID_7:
			setBooleanValue(SOUND_STRING, sib7.setChange());
			break;
		default:
			break;
		}

	}

	public void setStringValue(String key, String value) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(SETTING, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public String getStringValue(String key) {
		SharedPreferences settings = getApplicationContext()
				.getSharedPreferences(SETTING, 0);
		String value = settings.getString(key, null);
		return value;
	}

	public boolean getBooleanValue(String key) {
		SharedPreferences settings = getSharedPreferences(SETTING, 0);
		boolean value = settings.getBoolean(key, true);
		return value;
	}

	public void setBooleanValue(String key, boolean value) {
		SharedPreferences settings = getSharedPreferences(SETTING, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public int getIntValue(String key) {
		SharedPreferences settings = getSharedPreferences(SETTING, 0);
		int value = 0;

		try {
			value = settings.getInt(key, 0);
		} catch (Exception e) {
			try {
				String sValue = settings.getString(key, null);
				if (null != sValue)
					value = Integer.parseInt(sValue);
			} catch (Exception e1) {

			}
		}
		return value;
	}

	public void setIntValue(String key, int value) {
		SharedPreferences settings = getSharedPreferences(SETTING, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(key, value);
		editor.commit();
	}

}
